/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 9/30/2010
 * Time: 4:11 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 * make note manager
 * - have many tab for many project
 *   - each tab have many row 
 *     - each row is one task, have deadline (optional), have check for done  
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.IO;

namespace TaskNoteManager
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		string save_dir = FileLib.getFullPath("Data");
		
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		
			LoadAllNote();
			
			
		}	
		
		void LoadAllNote(){
			if( !Directory.Exists(save_dir) ) Directory.CreateDirectory(save_dir);		
			
			tab_container.TabPages.Clear();
			string[] files = FileLib.getFileList( save_dir, "*.xml", false, FileLib.SearchOption.FileOnly );
			
			if( files.Length == 0 ){
				addTask( FileLib.getFullPath("Data\\temp.xml") );
			}
			
			foreach( string file in files ){
				addTask(file);
			}
			
			TabPage NewEventTab = new TabPage("[New]");
			tab_container.TabPages.Add(NewEventTab);
			
			tab_container.SelectedIndexChanged+= delegate { 
				if( tab_container.SelectedTab == NewEventTab ){
					tab_container.TabPages.Remove(NewEventTab);
					int counter = 1;
					while(File.Exists(FileLib.getFullPath("Data\\Untitle"+counter+".xml")) )
						counter++;
					
					addTask( FileLib.getFullPath("Data\\Untitle"+counter+".xml") );
					tab_container.TabPages.Add(NewEventTab);
					tab_container.SelectTab( tab_container.TabPages.Count-2 );
				}
			};
		}
		
		void addTask(string file){
			TaskTabPage tab = new TaskTabPage( file );
			tab_container.TabPages.Add(tab);
		}
		
		void KeyUpEvent(object sender, KeyEventArgs e)
		{
			if( e.Control && e.KeyCode == Keys.S ){
				for( int i = 0; i<tab_container.TabPages.Count-1; i++ ){					
					((TaskTabPage)tab_container.TabPages[i]).SaveData();
				}
			}
		}
		
		
		void Tab_containerMouseDoubleClick(object sender, MouseEventArgs e)
		{
			if( e.Button == MouseButtons.Left ){
				txt_tabname.SetText( tab_container.SelectedTab.Text.Replace("*",""), tab_container.GetTabRect(tab_container.SelectedIndex) );				
				txt_tabname.Show();
				txt_tabname.Focus();
				txt_tabname.Select();
				txt_tabname.SelectAll();
			}else if( e.Button == MouseButtons.Right && 
			         FormLib.confirm("Are you sure you want to remove this task ["+tab_container.SelectedTab.Text.Replace("*","")+"] ?") )
			{
				((TaskTabPage)tab_container.SelectedTab).Remove();
				tab_container.TabPages.Remove( tab_container.SelectedTab );
			}
			
		}
		
		void Txt_tabnameChangeTabNameEvent()
		{
			TaskListGrid control = (TaskListGrid)tab_container.SelectedTab.Controls[0];			
			if( control.ChangeName( FileLib.getFullPath("Data\\"+txt_tabname.Text.Trim()+".xml") ) ){
				tab_container.SelectedTab.Text = txt_tabname.Text.Trim();
			}
		}	
	}
}
